using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using IView;
using System.Windows.Forms;
using Calendar;
using Presenter;

using DDay.iCal;
using DDay.iCal.Components;
using DDay.iCal.DataTypes;
using DataTransferObject.DiaryAndTrackingTask.SupportClassForDiary;
using IView.DiaryAndTracking;

namespace JSKE
{
    public partial class CalendarForm : FormLang, ICaldendarForm
    {
        #region Attribtutes
        public const int SHOW_DIARY = 0;
        public const int SHOW_SCHED_CALENDAR = 1;
        private List<Appointment> m_Appointments;
        private List<Event> events;

        private IMainForm _mainForm;
        private List<JskeEvent> _jskeEvents = new List<JskeEvent>();
        private IDiaryForm _diaryForm;
        private List<Color> tableColor;
        private List<String> _eventType;
        private PresenterCalendarForm _presenter;
        private int MODE;

        #endregion

        #region Properties
        public List<Appointment> Appointments
        {
            get { return m_Appointments; }
            set { m_Appointments = value; }
        }

        #endregion

        #region Essential Methods

        private void CreateTableColor()
        {
            tableColor = new List<Color>(16);
            tableColor.Add(Color.FromArgb(0, 127, 0));
            tableColor.Add(Color.FromArgb(255, 0, 0));
            tableColor.Add(Color.FromArgb(0, 0, 255));
            tableColor.Add(Color.FromArgb(0, 255, 0));
            tableColor.Add(Color.FromArgb(127, 127, 0));
            tableColor.Add(Color.FromArgb(255, 127, 0));
            tableColor.Add(Color.FromArgb(50, 255, 0));
            tableColor.Add(Color.FromArgb(0, 0, 127));
            tableColor.Add(Color.FromArgb(127, 0, 127));
            tableColor.Add(Color.FromArgb(127, 127, 127));
            tableColor.Add(Color.FromArgb(127, 0, 0));
            tableColor.Add(Color.FromArgb(255, 127, 127));
            tableColor.Add(Color.FromArgb(255, 255, 127));
            tableColor.Add(Color.FromArgb(100, 0, 200));
            tableColor.Add(Color.FromArgb(127, 0, 200));
            tableColor.Add(Color.FromArgb(0, 255, 200));
            tableColor.Add(Color.FromArgb(255, 0, 200));
            tableColor.Add(Color.FromArgb(0, 127, 150));
            tableColor.Add(Color.FromArgb(255, 255, 0));
            tableColor.Add(Color.FromArgb(140, 5, 100));
            tableColor.Add(Color.FromArgb(150, 15, 90));
            tableColor.Add(Color.FromArgb(160, 25, 80));
            tableColor.Add(Color.FromArgb(170, 35, 70));
            tableColor.Add(Color.FromArgb(180, 45, 60));
            tableColor.Add(Color.FromArgb(190, 55, 50));
            tableColor.Add(Color.FromArgb(210, 65, 40));
            tableColor.Add(Color.FromArgb(220, 75, 30));
            tableColor.Add(Color.FromArgb(230, 85, 20));
            tableColor.Add(Color.FromArgb(240, 95, 10));
        }

        private void defaultInitialize()
        {
            CreateTableColor();
            rdbWeek.Checked = true;
            m_Appointments = new List<Appointment>();

            DateTime m_Date = DateTime.Now;

            m_Date = m_Date.AddHours(10 - m_Date.Hour);
            m_Date = m_Date.AddMinutes(-m_Date.Minute);

            dayViewCalendar.StartDate = DateTime.Now;
            dayViewCalendar.NewAppointment += new NewAppointmentEventHandler(dayView1_NewAppointment);
            dayViewCalendar.SelectionChanged += new EventHandler(dayView1_SelectionChanged);
            dayViewCalendar.ResolveAppointments += new Calendar.ResolveAppointmentsEventHandler(this.dayView1_ResolveAppointments);

            dayViewCalendar.MouseMove += new System.Windows.Forms.MouseEventHandler(this.dayView1_MouseMove);

            comboBoxTheme.SelectedIndex = 1;
        }

        private int getColorNumber(String findC, List<String> ocean)
        {
            int result = 0;
            for (int i = 0; i < ocean.Count; i++)
            {
                if (findC.Trim().Equals(ocean[i].Trim()))
                {
                    return i;
                }
            }

            return result;
        }

        private void loadJskeEvt2Calendar(List<JskeEvent> allEvts)
        {
            
            for (int i = 0; i < allEvts.Count; i++)
            {
                Appointment m_Appointment = new Appointment();
                m_Appointment.StartDate = allEvts[i].EventStart;
                m_Appointment.EndDate = allEvts[i].EventStart.AddMinutes(allEvts[i].EventDuration);
                m_Appointment.Title = "[" + allEvts[i].EventType + "] - " + allEvts[i].EventName;
                m_Appointment.Color = tableColor[getColorNumber(allEvts[i].EventType, _eventType)];

                m_Appointments.Add(m_Appointment);
            }
        }

        private void loadJskeEventFromFile2Calendar()
        {
            _jskeEvents = _presenter.getListJskeEvent(DateTime.Now.Date.AddYears(-30), DateTime.Now.Date.AddYears(30));
            if (_jskeEvents != null)
            {
                _eventType = _presenter.getEventType().TypeName;
                loadJskeEvt2Calendar(_jskeEvents);
            }
        }

        private void loadSchedJskeEvent()
        {
            events = new List<Event>();
            _jskeEvents = _presenter.getScheduleCalendar();
            iCalendar iCal = new iCalendar();


            if (_jskeEvents != null)
            {
                m_Appointments.Clear();
                for (int i = 0; i < _jskeEvents.Count; i++)
                {
                    Appointment m_Appointment = new Appointment();
                    Event new1stEvent = iCal.Create<Event>();

                    m_Appointment.StartDate = _jskeEvents[i].EventStart;
                    m_Appointment.EndDate = _jskeEvents[i].EventStart.AddMinutes(_jskeEvents[i].EventDuration);
                    m_Appointment.Title = _jskeEvents[i].EventType + " - " + _jskeEvents[i].EventName;
                    m_Appointment.Color = Color.Gold;
                    m_Appointments.Add(m_Appointment);

                    new1stEvent.DTStart = new DDay.iCal.DataTypes.iCalDateTime(_jskeEvents[i].EventStart);
                    new1stEvent.DTEnd = new DDay.iCal.DataTypes.iCalDateTime(_jskeEvents[i].EventStart.AddMinutes(_jskeEvents[i].EventDuration));
                    new1stEvent.Summary = _jskeEvents[i].EventType + " - " + _jskeEvents[i].EventName;

                    events.Add(new1stEvent);
                }

            }

        }

        #endregion

        #region Constructors

        public CalendarForm(IDiaryForm DiaryFormView)
        {
            //InitializeComponent();
            defaultInitialize();
            _presenter = new PresenterCalendarForm(DiaryFormView, this, DATA_MODE);

            loadJskeEventFromFile2Calendar();
            _diaryForm = DiaryFormView;
        }

        public CalendarForm()
        {
            //InitializeComponent();
            defaultInitialize();
            _presenter = new PresenterCalendarForm(this, DATA_MODE);

            loadJskeEventFromFile2Calendar();
        }

        public CalendarForm(IMainForm MainFormView)
        {
            //InitializeComponent();
            defaultInitialize();
            this._mainForm = MainFormView;

        }

        public CalendarForm(int mode, IMainForm MainFormView)
        {
            // TODO: Complete member initialization
            this.MODE = mode;
            this._mainForm = MainFormView;
            defaultInitialize();
            _presenter = new PresenterCalendarForm(this, DATA_MODE);

            if (MODE == SHOW_SCHED_CALENDAR)
            {
                loadSchedJskeEvent();
            }
        }

        #endregion

        #region Events Handler
        void dayView1_NewAppointment(object sender, NewAppointmentEventArgs args)
        {
            Appointment m_Appointment = new Appointment();

            m_Appointment.StartDate = args.StartDate;
            m_Appointment.EndDate = args.EndDate;
            m_Appointment.Title = args.Title;

            m_Appointments.Add(m_Appointment);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            //dayView1.DaysToShow = int.Parse( textBox1.Text );
        }

        private void dayView1_MouseMove(object sender, MouseEventArgs e)
        {

        }

        private void dayView1_SelectionChanged(object sender, EventArgs e)
        {

        }

        private void dayView1_ResolveAppointments(object sender, ResolveAppointmentsEventArgs args)
        {
            List<Appointment> m_Apps = new List<Appointment>();

            foreach (Appointment m_App in m_Appointments)
                if ((m_App.StartDate >= args.StartDate) &&
                    (m_App.StartDate <= args.EndDate))
                    m_Apps.Add(m_App);
            args.Appointments = m_Apps;
        }

        private void butCreate_Click(object sender, EventArgs e)
        {
            Appointment m_App = new Appointment();
            m_App.StartDate = dayViewCalendar.SelectionStart;
            m_App.EndDate = dayViewCalendar.SelectionEnd;
            m_App.BorderColor = Color.Red;
            m_Appointments.Add(m_App);

            dayViewCalendar.Invalidate();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }


        private void comboBoxTheme_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBoxTheme.SelectedItem.ToString() == "Office 11")
            {
                dayViewCalendar.Renderer = new Office11Renderer();
            }
            else
            {
                dayViewCalendar.Renderer = new Office12Renderer();
            }
        }

        private void butColor_Click(object sender, EventArgs e)
        {
            if (dayViewCalendar.SelectedAppointment != null)
            {
                colorDialog1.Color = dayViewCalendar.SelectedAppointment.Color;

                if (colorDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    dayViewCalendar.SelectedAppointment.Color = colorDialog1.Color;
                }
            }
        }

        private void butBorder_Click(object sender, EventArgs e)
        {
            if (dayViewCalendar.SelectedAppointment != null)
            {
                colorDialog1.Color = dayViewCalendar.SelectedAppointment.BorderColor;

                if (colorDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    dayViewCalendar.SelectedAppointment.BorderColor = colorDialog1.Color;
                }
            }
        }

        private void zoomBar_Scroll(object sender, EventArgs e)
        {
            dayViewCalendar.HalfHourHeight = zoomBar.Value;
        }

        private void monthCalendar_DateChanged(object sender, DateRangeEventArgs e)
        {
            dayViewCalendar.StartDate = monthCalendar.SelectionStart;
        }

        private void butExportOutlook_Click(object sender, EventArgs e)
        {

            if (this.MODE == SHOW_SCHED_CALENDAR)
            {
                _mainForm.WriteEventToOutlook(events);
            }
            else
            {
                events = _mainForm.GetListEvents();
                _mainForm.WriteEventToOutlook(events);
            }
        }

        private void rdbDay_CheckedChanged(object sender, EventArgs e)
        {
            dayViewCalendar.DaysToShow = 1;
        }

        private void rdbWeek_CheckedChanged(object sender, EventArgs e)
        {
            dayViewCalendar.DaysToShow = 7;
        }

        private void rdbMonth_CheckedChanged(object sender, EventArgs e)
        {
            dayViewCalendar.DaysToShow = 28;
        }

        #endregion
    }
}